﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace CommonHelper
{
    /// <summary>
    /// 开机自启帮助类
    /// </summary>
    public static class AppAutoRunHelper
    {
        /// <summary>
        /// 设置开机自启
        /// </summary>
        /// <param name="KeyName"></param>
        public static void SetAppAutoRun(string KeyName = "AutoRunApp")
        {
            string AppPath = Assembly.GetEntryAssembly().Location;
            AppPath = AppPath.Replace(".dll", ".exe");
            RegistryKey RKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            RKey.SetValue(KeyName, AppPath);
        }

        /// <summary>
        /// 删除开机自启
        /// </summary>
        /// <param name="KeyName"></param>
        public static void DeleteAppAutoRun(String KeyName = "AutoRunApp")
        {
            try
            {
                string AppPath = Assembly.GetEntryAssembly().Location;
                Registry.CurrentUser.DeleteSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            }
            catch
            {

            }
        }
    }
}
